package com.xpgk.mvc.ddc.bgdj.ywxg;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.ddc.ddc_commomUtil.PictureService;
import com.xpgk.mvc.ddc.entity.Business;
import com.xpgk.mvc.ddc.entity.Credentials;
import com.xpgk.mvc.ddc.entity.Owner;
import com.xpgk.mvc.ddc.entity.Plate;
import com.xpgk.mvc.ddc.entity.Vehicle;
import com.xpgk.mvc.ddc.entity.VehicleParemeters;
import com.xpgk.mvc.ddc.statistic.DailyStasticService;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.user.User;


public class BgdjYwxgController  extends  BaseController{
	private DailyStasticService dailyStasticService;
	private PictureService pictureService = new PictureService();
	
	public void index() {
		if(!getCUserOrganIds().equals("f73bfba518ff4048afbdc006e3eda906")){
			setAttr("orgid", getCUserOrganIds());
		}
		render("ywxglist.html");
	}
	
	public  void  getlist(){
		String json = paging(splitPage, Business.sqlId_splitPageSelect,Business.sqlId_splitPageFrom);
		renderJson(json);
	}
	
	public  void  details(){
		ids=getPara("ids");
		Business business = Business.dao.findById(ids);
		setAttr("business", business);
		setAttr("owner", Owner.dao.findById(business.get("NEWOWNERIDS")));
		setAttr("vehicleparemeters", VehicleParemeters.dao.findById(business.get("NEWPAREMETERSIDS")));
		setAttr("vehicleparemeters2", VehicleParemeters.dao.findById(business.get("OLDPAREMETERSIDS")));
		setAttr("plate", Plate.dao.findById(business.get("NEWPLATEIDS")));
		setAttr("vehicle", Vehicle.dao.findById(business.get("VEHICLEIDS")));
		setAttr("credentials", Credentials.dao.findById(business.get("NEWCREDENTIALSIDS")));
		render("details.html");
	}

	@Before(Tx.class)
	public  void  nextprocess(){
		String ids = getPara("ids");
		Business business = Business.dao.findById(ids);
		User user = getCUser();
		String jbrids = user.getStr(User.column_ids);
		String jbr = user.getStr(User.column_name);
		String organids = user.getStr(User.column_organizationids);
		Organization organization = Organization.dao.findById(organids);
		String organ = organization.getStr(Organization.column_name);
		SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");
		business.set(Business.column_YWXGDATE, sdf.format(new Date()));
		business.set(Business.column_YWXGJBRIDS, jbrids);
		business.set(Business.column_YWXGJBR, jbr);
		business.set(Business.column_STATUS,"2");
		boolean result = business.update();
		result = dailyStasticService.saveDailyStastic(organids,organ,sdf.format(new Date()),"3","2");
		renderJson(result);
	}
	
	@Before(Tx.class)
	@SuppressWarnings("static-access")
	public  void  update(){	
		RenderBean result = new RenderBean();
		
		try {
			User user = getCUser();
			String jbrids = user.getStr(User.column_ids);
			String jbr = user.getStr(User.column_name);
			
			SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			Owner  owner = getModel(Owner.class,"owner");
			owner.update();
			
			Plate  plate = getModel(Plate.class,"plate");
//		plate.set(Plate.column_HPHM, "晋B"+plate.get("HPHM"));
			plate.update();
			
			VehicleParemeters  vehicleparemeters = getModel(VehicleParemeters.class,"vehicleparemeters");
			VehicleParemeters  vehicleparemeters2 = VehicleParemeters.dao.findById(vehicleparemeters.get("IDS"));
			vehicleparemeters.set(VehicleParemeters.column_HPHM, plate.get("HPHM"));
			vehicleparemeters.set(vehicleparemeters.column_CLLX, vehicleparemeters2.get("CLLX"));
			vehicleparemeters.set(vehicleparemeters.column_SYXZ, vehicleparemeters2.get("SYXZ"));
			vehicleparemeters.set(vehicleparemeters.column_CSYS, vehicleparemeters2.get("CSYS"));
			vehicleparemeters.update();
			
			Vehicle vehicle = getModel(Vehicle.class,"vehicle");
			vehicle.set(Vehicle.column_HPHM, plate.get("HPHM"));
//		vehicle.set(Vehicle.column_OWNERNAME, owner.get("OWNERNAME"));
			vehicle.update();
			
			Credentials credentials = getModel(Credentials.class,"credentials");
			credentials.set(Credentials.column_HPHM, plate.get("HPHM"));
			credentials.set(credentials.column_OWNERNAME, owner.get("OWNERNAME"));
//		credentials.set(Credentials.column_COMPANYNAME, owner.get("COMPANYNAME"));
			credentials.set(Credentials.column_ADDRESS, owner.get("ADDRESS"));
			credentials.set(Credentials.column_DETAILADDRESS, owner.get("DETAILADDRESS"));
			credentials.update();
			
			Business  business = getModel(Business.class,"business");
//		business.set(Business.column_NEWOWNERNAME, owner.get("OWNERNAME"));
			business.set(Business.column_NEWHPHM, plate.get("HPHM"));
			business.set(Business.column_YWXGDATE, sdf.format(new Date()));
			business.set(Business.column_YWXGJBRIDS, jbrids);
			business.set(Business.column_YWXGJBR, jbr);
			business.update();
			
			String picStr1 = getPara("picStr1");
			String picStr2 = getPara("picStr2");
			String picStr3 = getPara("picStr3");
			String picStr4 = getPara("picStr4");
			String BUSINESSIDS = business.get("IDS");
			String VEHICLEIDS = vehicle.get("IDS");
			if(picStr1.length()>0){
				pictureService.uploadPhoto(picStr1,BUSINESSIDS,VEHICLEIDS,"PICTURE1","PICTURE1");
			}
			if(picStr2.length()>0){
				pictureService.uploadPhoto(picStr2,BUSINESSIDS,VEHICLEIDS,"PICTURE2","PICTURE2");
			}
			if(picStr3.length()>0){
				pictureService.uploadPhoto(picStr3,BUSINESSIDS,VEHICLEIDS,"PICTURE3","PICTURE3");
			}
			if(picStr4.length()>0){
				pictureService.uploadPhoto(picStr4,BUSINESSIDS,VEHICLEIDS,"PICTURE4","PICTURE4");
			}
			result.setCode(ConstantRender.render_success_code);
			result.setMsg("保存成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setCode(ConstantRender.render_error_code);
			result.setMsg("保存失败");
		}
		renderJson(result);
	}
	
	

	
}
